/**
 * @file Thread.hh
 * @author zziywang (zziywang@163.com)
 * @brief { life is too short to learn cpp! }
 * @version 1.0
 * @date 2022-05-08
 *
 * @copyright Copyright (c) 2022
 *
 */

#pragma once
#include "Noncopyable.hh"
#include <functional>
#include <pthread.h>
#include <string>
using std::string;

namespace snow
{

namespace tls
{
extern __thread const char *threadName; // 外部变量声明
} // namespace tls

using ThreadCallBack = std::function<void()>; // 返回值类型void，无参
class Thread
    : Noncopyable
{
public:
    Thread(const ThreadCallBack &cb, const string &name = string());
    ~Thread();

    void start(); // 创建线程
    void join();  // 等待回收

    static void *threadFunc(void *arg); // 线程入口函数必须是void *threadFunc(void *arg);

private:
    pthread_t pthid_;
    bool isRunning_;
    ThreadCallBack cb_; // 具体任务，以回调函数形式传给线程
    string name_;
};

} // namespace snow